﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Ecreate.Model;
using Ecreate.Data;
namespace Ecreate.Business
{
    public class CustomerService
    {
        public string getXmlString()
        {
            ArrayList l = (ArrayList)FactoryDBEngine.getIBatisService().ExecuteQueryForList("t_area_SelectUse", null);
            ArrayList l_data = (ArrayList)FactoryDBEngine.getIBatisService().ExecuteQueryForList("t_area_SelectAll", null);
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version='1.0' encoding='gb2312'?>");
            xml.Append("<tree id='0'>");
            foreach (t_area p in l)
            {
                if (p.ParentAreacode == "-1")
                {
                    beginItem(p, xml);
                    getChildXml(p, xml, l_data);
                    endItem(xml);
                }
            }
            xml.Append("</tree>");
            return xml.ToString();
        }
        private void getChildXml(t_area p, StringBuilder xml, ArrayList data)
        {
            foreach (t_area pitem in data)
            {
                if (pitem.ParentAreacode == p.AreaCode)
                {
                    beginItem(pitem, xml);
                    getChildXml(pitem, xml, data);
                    endItem(xml);
                }
            }
        }
        private void beginItem(t_area p, StringBuilder xml)
        {
            string img = "";
            string state = "";
            if (p.ParentAreacode == "-1")
            {
                state = " select='1' open='1' ";
            }
            else
            {
            }
            xml.Append((new StringBuilder("<item id='")).Append(p.AreaCode).Append("' text='").Append(p.AreaName).Append("' ").Append(img).Append(state).Append(">"));
            xml.Append("<userdata name='lastflag'>");
            xml.Append(p.LastFlag.ToString());
            xml.Append("</userdata>");
        }
        private void endItem(StringBuilder xml)
        {
            xml.Append("</item>");
        }


        public void Delete(t_customer t_cust)
        {
            FactoryDBEngine.getIBatisService().Delete("t_customer_Delete", t_cust.Cus_id);
        }

        public void Update(t_customer t_cust)
        {

            FactoryDBEngine.getIBatisService().Update("t_customer_Update", t_cust);
        }

        public WebResult<object> UpdatePassword(t_customer model)
        {
            WebResult<object> result = new WebResult<object>();
            t_customer search = FactoryDBEngine.SqlMapper().QueryForObject<t_customer>("t_customer_SelectByID", model.Cus_id);
            if (search == null)
            {
                result.Message = "删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                {
                    FactoryDBEngine.SqlMapper().Update("t_customer_UpdatePassword", model);
                    result.Success = true;
                    result.Message = "修改密码成功!";
                }
            }
            return result;
        }



        public WebResult<t_customer> Insert(t_customer t_cust)
        {
            WebResult<t_customer> result = new WebResult<t_customer>();
            try
            {
                FactoryDBEngine.SqlMapper().Insert("t_customer_Insert", t_cust);
                result.ResultObject = t_cust;
                result.Success = true;
            }
            catch
            {
                result.ResultObject = null;
                result.Success = false;
                result.Message = "供应商编码重复!";
            }
            return result;
        }


        public IList<t_customer> getCustomersByAreaCode(string AreaCode)
        {
            IList<t_customer> l = FactoryDBEngine.SqlMapper().QueryForList<t_customer>("t_customer_SelectAllByAreaCode", AreaCode);
            return l;
        }

        public t_customer getCustomerByID(string areaCode)
        {

            return FactoryDBEngine.SqlMapper().QueryForObject<t_customer>("t_customer_SelectByID", areaCode);
        }
        public IList<t_customer> getAll()
        {

            return FactoryDBEngine.SqlMapper().QueryForList<t_customer>("t_customer_SelectAll", null);


        }

        public ArrayList getCustomersSelectAll(string AreaCode)
        {
            return (ArrayList)FactoryDBEngine.getIBatisService().ExecuteQueryForList("t_customer_SelectAll", AreaCode);

        }


        public WebResult<t_customer> checkPassword(string id,string password)
        {
            WebResult<t_customer> result = new WebResult<t_customer>();
            result = this.getResultByID(id);
            if (result.Success)
            {
                if (result.ResultObject.Password != password)
                {
                    result.Success = false;
                    result.Message = "密码错误!";
                }
            }
            else
            {
                result.Message = "用户名或密码输入错误";
            }

            return result;
        }
        public WebResult<t_customer> getResultByID(string id)
        {
            WebResult<t_customer> result = new WebResult<t_customer>();
            t_customer search = FactoryDBEngine.SqlMapper().QueryForObject<t_customer>("t_customer_SelectByID", id);
            if (search == null)
            {
                result.Message = "删除对像不存在,请刷新列表";
                result.Success = false;
            }
            else
            {
                result.Success = true;
                result.ResultObject = search;
            }
            return result;

        }














    }
}
